#ifndef DATASTRUCTURE_LINKEDLIST_BINARY_TREE_H
#define DATASTRUCTURE_LINKEDLIST_BINARY_TREE_H

#include "BinaryTree.h"

typedef BinaryTree NodeData;

typedef struct LinkedListBinaryTreeNode {
    NodeData *data;
    // 直接后继节点
    struct LinkedListBinaryTreeNode *next;
} LinkedListBinaryTree;

int verificationLinkedListBinaryTree(LinkedListBinaryTree *linkedListBinaryTree) {
    return linkedListBinaryTree == NULL ? 0 : 1;
}

LinkedListBinaryTree *createLinkedListBinaryTree(NodeData *e) {
    LinkedListBinaryTree *node = malloc(sizeof *node);
    node->next = NULL;
    node->data = e;
    return node;
}

LinkedListBinaryTree *initLinkedListBinaryTree() {
    // 初始化头尾指针
    LinkedListBinaryTree *head = createLinkedListBinaryTree(NULL);
    return head;
}

// 入队列
int enBinaryTreeQueue(LinkedListBinaryTree *queue, NodeData *e) {
    if (!verificationLinkedListBinaryTree(queue)) {
        return 0;
    }

    LinkedListBinaryTree *p = queue;
    while (p->next != NULL) {
        p = p->next;
    }
    LinkedListBinaryTree *newNode = createLinkedListBinaryTree(e);
    p->next = newNode;
    return 1;
}

// 出队列
NodeData *deBinaryTreeQueue(LinkedListBinaryTree *queue) {
    if (!verificationLinkedListBinaryTree(queue)) {
        return NULL;
    }
    LinkedListBinaryTree *p = queue;
    NodeData *data = NULL;
    if (p->next != NULL) {
        LinkedListBinaryTree *temp = p->next;
        p->next = p->next->next;
        data = temp->data;
        free(temp);
    }
    return data;
}

// 释放内存
void deleteLinkedListBinaryTree(LinkedListBinaryTree *queue){
    if (queue == NULL) {
        return;
    }
    LinkedListBinaryTree *p = queue;
    while (p != NULL) {
        LinkedListBinaryTree *temp = p;
        p = p->next;
        free(temp);
    }
}
#endif // DATASTRUCTURE_LINKEDLIST_BINARY_TREE_H